package com.etc.controller;

import com.alibaba.fastjson.JSON;
import com.etc.dto.OrderAndAddress;
import com.etc.entity.OrderDetails;
import com.etc.service.impl.OrderServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@SessionAttributes(value = {"order","revise"})
@RequestMapping("/order")
@Controller
public class OrderController {
    @Resource
    private OrderServiceImpl orderService;
    @RequestMapping("quer")
    public ModelAndView queryAllOrder(Integer id,Integer osid){
        ModelAndView mv=new ModelAndView();
     List<OrderAndAddress> orders= orderService.queryAllOrder(id,osid);

        mv.addObject("order",orders);
        mv.setViewName("redirect:../product_Manage.jsp");
        return mv;
    }


  //  @RequestMapping(value="/querid")
   // @RequestMapping(value="/querid",produces="application/json;charset=UTF-8")
  @RequestMapping(value = "/querid",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String queryIdOrder(Integer id){
        System.out.println(id);
        List<OrderAndAddress> orderAndAddresses =orderService.queryIdOrder(id);
        System.out.println("ksksksks");
        System.out.println(orderAndAddresses.toString());
        return   JSON.toJSONString(orderAndAddresses);


    }

    @RequestMapping("revise")
    public ModelAndView modifyState(Integer dingdanid,Integer osid,Integer sjid){
        ModelAndView mv=new ModelAndView();
        System.out.println("xiugai");
       int con= orderService.modifyState(dingdanid,osid);
     if (con==1){
         mv.addObject("revise","发货成功");
     }else {   mv.addObject("revise","发货失败");}

     if (osid==3){
         osid=2;
     }

        mv.setViewName("redirect:quer.html?id="+sjid+"&osid="+osid);
        return mv;
    }


    @RequestMapping(value = "/queryAllId",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public  String queryAllid(Integer goodsid){
        ModelAndView mv=new ModelAndView();
        List<OrderDetails> orderDetails = orderService.queryAllId(goodsid);
        return   JSON.toJSONString(orderDetails);

    }

}
